package com.bestcem.xm.user.controller;


import com.bestcem.xm.common.core.uitls.IpUtil;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.user.service.UserJobService;
import com.bestcem.xm.user.service.UserOrgBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 用户中心定时任务接口
 *
 * @author YiFang <yifang.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/8/17 17:22
 */
@Slf4j
@RequestMapping("/ds/job/user")
@RestController
public class UserJobController {


    @Resource
    UserJobService userJobService;

    @Resource
    private UserOrgBusinessService userOrgBusinessService;

    private static final String LOCAL_HOST_IPV6 = "0:0:0:0:0:0:0:1";

    /**
     * 预创建注册公司定时任务
     */
    @AuthIgnore
    @GetMapping("/create_pre_org")
    public String executeCreatePreOrgJob(HttpServletRequest request) {
        String ip = IpUtil.getIpAddress(request);
        if (!LOCAL_HOST_IPV6.equals(ip)) {
            return "只允许本机执行";
        }
        userJobService.executeCreatePreOrgJob();
        return "create_pre_org";
    }

    /**
     *
     */
    @AuthIgnore
    @GetMapping("/export_daily_visitor")
    public String executeExportDailyVisitorJob(HttpServletRequest request) {
        String ip = IpUtil.getIpAddress(request);
        if (!LOCAL_HOST_IPV6.equals(ip)) {
            return "只允许本机执行";
        }
        userJobService.executeExportDailyVisitorJob();
        return "export_daily_visitor";
    }

    @AuthIgnore
    @GetMapping("/package_demotion")
    public String executePackageDemotionJob(HttpServletRequest request) {
        String ip = IpUtil.getIpAddress(request);
        if (!LOCAL_HOST_IPV6.equals(ip)) {
            return "只允许本机执行";
        }
        userJobService.executePackageDemotionJob();
        return "package_demotion";
    }

    /**
     * 公司套餐30天临期提醒任务 async_temporary_reminder
     */
    @AuthIgnore
    @GetMapping("/package_temporary_reminder")
    public String executePackageTemporaryReminderJob(HttpServletRequest request) {
        String ip = IpUtil.getIpAddress(request);
        if (!LOCAL_HOST_IPV6.equals(ip)) {
            return "只允许本机执行";
        }
        userJobService.executePackageTemporaryReminderJob();
        return "package_temporary_reminder";
    }

    /**
     * 每三个月向growingio上传一次登录用户变量定时任务
     */
    @AuthIgnore
    @GetMapping("/upload_growingio")
    public String executeUploadGrowingioJob(HttpServletRequest request) {
        String ip = IpUtil.getIpAddress(request);
        if (!LOCAL_HOST_IPV6.equals(ip)) {
            return "只允许本机执行";
        }
        userJobService.executeUploadGrowingioJob();
        return "package_temporary_reminder";
    }

    /**
     * 预创建
     */
    @AuthIgnore
    @GetMapping("/org/create")
    public String preCreateOrg(HttpServletRequest request) {
        String ip = IpUtil.getIpAddress(request);
        if (!LOCAL_HOST_IPV6.equals(ip)) {
            return "只允许本机执行";
        }
        if (log.isInfoEnabled()) {
            log.info("[USER] 预创建注册公司定时任务开始");
        }
        try {
            userOrgBusinessService.createPreOrgForRegister();
        } catch (Exception e) {
            log.error("[USER] {} 预创建注册公司定时任务失败; ErrorMsg: {}", e.getMessage(), e);
        }
        if (log.isInfoEnabled()) {
            log.info("[USER] 预创建注册公司定时任务结束");
        }
        return "preCreateOrg success!";
    }
}
